package com.machine.action.buycomponent;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.machine.bean.BuyComponent;
import com.machine.bean.PageBean;
import com.machine.service.user.BuyComponentServiceDao;
import com.opensymphony.xwork2.ActionSupport;

@Component("findHasOutBuyComAction")
@Scope(value = "prototype")
public class FindHasOutBuyComAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private BuyComponentServiceDao buyComponentServiceDao;

	private String username;
	private List<BuyComponent> buyComponents = null;

	private int page; // 第几页

	private PageBean pageBean; // 包含分布信息的bean

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public BuyComponentServiceDao getBuyComponentServiceDao() {
		return buyComponentServiceDao;
	}

	@Resource(name = "buyComponentService")
	public void setBuyComponentServiceDao(
			BuyComponentServiceDao buyComponentServiceDao) {
		this.buyComponentServiceDao = buyComponentServiceDao;
	}

	public List<BuyComponent> getBuyComponents() {
		return buyComponents;
	}

	public void setBuyComponents(List<BuyComponent> buyComponents) {
		this.buyComponents = buyComponents;
	}

	@Override
	public String execute() throws Exception {

		if (page <= 0) {
			page = 1;
		}

		pageBean = buyComponentServiceDao.queryForPage(5, page, username);
		buyComponents = pageBean.getList();
		System.out.println(username);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out
				.print("<form method='post' class='outinfoform' ><table class='back_table' cellspacing='0' cellpadding='0'>");
		out.print("<tr><th width='30'>选择</th><th>名称</th><th>寻购信息</th></tr>");

		for (int i = 0; i < buyComponents.size(); i++) {
			out.print("<tr>");
			out.print("<th width='20'><input name='buyComponentId' value='"
					+ buyComponents.get(i).getId() + "' type='checkbox'></th>");
			out.print("<th>");
			out.print(buyComponents.get(i).getName());
			out.print("</th>");

			out.print("<th>");
			out.print(buyComponents.get(i).getInfo());
			out.print("</th>");
			out.print("</tr>");
		}

		out.print("<tr>");
		out.print("<td>");
		if (pageBean.getCurrentPage() == 1) {
			out.print("上一页	首页");
		} else {
			out.print("<a onclick='return changeframe(1);'>首页</a>");

			out.print("<a onclick='return changeframe("
					+ (pageBean.getCurrentPage() - 1) + ");' >上一页</a>");
		}

		out.print("</td>");
		out.print("<td>");
		if (pageBean.getCurrentPage() != pageBean.getTotalPage()) {

			out.print("<a onclick='return changeframe("
					+ (pageBean.getCurrentPage() + 1) + ");' >下一页</a>");

			out.print("<a onclick='return changeframe("
					+ pageBean.getTotalPage() + ");'>尾页</a>");
		} else {
			out.print("下一页	尾页");
		}

		out.print("共" + pageBean.getAllRow() + "条记录");
		out.print("</td>");
		out.print("</tr>");
		out.print("</table>");
		out.print("</form>");
		out.flush();
		out.close();

		return SUCCESS;

	}

}
